﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using System.Collections;
using System.Data;

namespace logicunivesityinventorystore.Department
{ 
    
    public partial class PendingRejectedPopUp : System.Web.UI.Page
    {
        static List<GetRetrivalDetails> reqDetail;
        static string formID;
        RequisitionFacade reqFac = new RequisitionFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                reqDetail = new List<GetRetrivalDetails>();
                formID = Request.QueryString["formID"];
                Requisition req = reqFac.FindRequisitionFormById(formID);
                var requi = reqFac.getRequsitionDetails(formID);
                foreach (GetRetrivalDetails reDet in requi)
                {
                    reqDetail.Add(reDet);
                }
                string s = req.RemarksByHead;
                if (s != null)
                {
                    txtRemarks.Visible = true;
                    txtRemarks.Text = s;
                }
            }
            BindGridView();
        }

        private void BindGridView()
        {
            gvOrderItems.DataSource = reqDetail;
            gvOrderItems.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<RequisitionDetail> ord = new List<RequisitionDetail>();
            foreach(GridViewRow row in gvOrderItems.Rows)
            {
                RequisitionDetail detail = new RequisitionDetail();
                detail.ItemCode=row.Cells[0].Text;
                detail.FormID=formID;
                detail.Qty=Convert.ToInt32(row.Cells[3].Text);
                ord.Add(detail);
            }
            new RequisitionFacade().addRequisitionDetails(ord,formID);
            BindGridView();
            ScriptManager.RegisterStartupScript(this,
                this.GetType(),
                "AlertScript",
                "alert('Your changes are sucessfully changed!');",
                 true);
            //change status to Pending
            //new RequisitionFacade().UpdateStatus(formID);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtQty.Text == "")
            {
                lblMsg.Text = "Please enter quantity!";
            }
            else
            {
                lblMsg.Text = " ";
                bool flag = true;
                GetRetrivalDetails addDetail = new GetRetrivalDetails();
                addDetail.ItemCode = ddlItem.SelectedValue;
                addDetail.Qty = txtQty.Text;
                addDetail.CategoryName = ddlCategory.SelectedItem.ToString();
                addDetail.ItemName = ddlItem.SelectedItem.ToString();
                foreach (GridViewRow gvr in gvOrderItems.Rows)
                {
                    if (ddlItem.SelectedValue.Equals(gvr.Cells[0].Text))
                    {
                        int inHand = Convert.ToInt32(gvr.Cells[3].Text);
                        int addValue = Convert.ToInt32(txtQty.Text);
                        //gvr.Cells[3].Text = (inHand + addValue).ToString();
                        GetRetrivalDetails add = new GetRetrivalDetails();
                        add = reqDetail.Find(delegate(GetRetrivalDetails r) { return r.ItemCode == (gvr.Cells[0].Text); });
                        add.Qty = (inHand + addValue).ToString();
                        flag = false;
                        break;
                    }
                }
                if (flag)
                    reqDetail.Add(addDetail);
                BindGridView();
            }
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtQty.Text = " ";
        }

        protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtQty.Text = " ";
        }



    }
}